package com.globalwave.common.appframe
{
	import com.globalwave.common.C;
	import com.globalwave.common.appframe.module.AppModule;
	
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.core.UIComponent;
	import mx.events.ModuleEvent;
	import mx.modules.IModuleInfo;
	import mx.modules.Module;
	import mx.modules.ModuleManager;
	
	import org.hamcrest.mxml.object.Null;

	public class ModuleLoader
	{
		private var container:UIComponent ;
		private var moduleInfo:IModuleInfo ;
		private var url:String ;
		private var isUnLoaded:Boolean = false ;
		
		public function ModuleLoader(container:UIComponent)
		{
			this.container = container ;
		}
				
		public function load(url : String) : void {
		    trace("module will be loaded:" + url);
			this.url = url ;
			if (moduleInfo == null) {
				//StyleManager.loadStyleDeclarations("com/globalwave/common/appframe/assets/Assets.swf",true,false);
				var moduleInfoTemp:IModuleInfo = ModuleManager.getModule(url);   
				if (C.IS_DEBUG) {
					if (!isUnLoaded && moduleInfoTemp.loaded) {
						
						moduleInfoTemp.addEventListener(ModuleEvent.UNLOAD, unloadOnFinish);
						//moduleInfoTemp.release();
						moduleInfoTemp.unload() ;
						return ;
					}
				}
				
				this.moduleInfo = moduleInfoTemp ;
				/*
				if (moduleInfo.data == null) {
					moduleInfo.data = new Object() ;
				}
				
				moduleInfo.data.id = id ;
				moduleInfo.data.label = label ;
				*/
				this.container.addChild(new Label()) ;
				
				moduleInfo.addEventListener(ModuleEvent.READY, loadOnReady);
				
				moduleInfo.addEventListener(ModuleEvent.PROGRESS, loadOnProgress);
				
				moduleInfo.addEventListener(ModuleEvent.ERROR, loadOnError);
			}
			
			this.container.cursorManager.setBusyCursor();
			moduleInfo.load();
			
		}
		
		private function loadOnReady(e:ModuleEvent):void {
			// Get an instance of the module.
			trace("module on ready");
			var moduleInfo:IModuleInfo = e.currentTarget as IModuleInfo;
			
			this.container.removeChildAt(0);
			var module:Module = moduleInfo.factory.create() as Module ;
			var appModule:AppModule = (module is AppModule)? (module as AppModule) : null;
			
			this.container.addChild(module) ;
			
			this.container.cursorManager.removeBusyCursor();
			
			if (this.container is SubFormInf) {
				(this.container as SubFormInf).setModule(appModule);
				(this.container as SubFormInf).loadMouldOnCompleted();
			} else if (this.container is ModuleWindow) {
				appModule.parentModule = (this.container as ModuleWindow).parentModule ;
			}
			
			//frameModuleLoader.removeAllChildren() ;
			//frameModuleLoader.addChild(frameModule) ;
			//addModule(moduleInfo.data.id, frameModule);
			//addTab(moduleInfo.data.label , frameModuleLoader, frameModule) ;
		}
		
		private function loadOnProgress(e:ModuleEvent):void {
			if (this.container.getChildAt(0) is Label) {
				(this.container.getChildAt(0) as Label).text = 
				    "ModuleEvent.PROGRESS received: " + e.bytesLoaded + " of " + e.bytesTotal + " loaded." ;
			}
		}
		
		private function loadOnError(e:ModuleEvent):void {
		    Alert.show("error:" + e.errorText) ;
			
			this.container.cursorManager.removeBusyCursor();
		}
		
		private function unloadOnFinish(e:ModuleEvent):void {
			isUnLoaded = true ;
			this.moduleInfo = null ;
			load(this.url) ;
		}
	}
}